<?php
if(!defined('BLOG'))exit('不作死就不会死');
	 /**
	 *  专业处理缩略图  验证码 
	 *  @author	Freax
	 *  @2014-3-24
	 *  @contact  huangyanxiong2013@gmail.com
	 *  
	 */

	class Images{
		/**
		 * 源图像的路径
		 * @var string
		 */
		public $sourceImagesaddr;
		/**
		 * 源图像的宽度
		 * @var int
		 */
		public $sourceImagesWidth;
		/**
		 * 源图像的高度
		 * @var int
		 */
		public $sourceImagesHeight;
		/**
		 * 指定压缩宽度
		 * @var int
		 */
		public $compressionImagesWidth;
		/**
		 * 指定压缩的高度
		 * @var int
		 */
		public $compressionImagesHieght;
		/**
		 * mine 类型用于header (Content-type:image/png) 随图片格式而去
		 * @var string
		 */
		public $mime;
		/**
		 * 图像的扩展名 png jpg gif 随源图像而改变
		 * @var string
		 */
		public $imagesExt;
		/**
		 * 中转的图片资源
		 * @var resouce
		 */
		public $imageTmp;
		/**
		 * 要输出的到文件的地址
		 * @var string
		 */
		public $outputfile;
		/**
		 * 压缩后的图片资源  用于指定输出到文件或者浏览器
		 * @var resouce
		 */
		public $imResouce;
		public function __construct($newImageWith,$newImageHeight){
			$this->getgdinfo();
			$this->compressionImagesHieght=$newImageHeight;
			$this->compressionImagesWidth=$newImageWith;
		}
		public function getgdinfo(){
			try{
				gd_info();
			}catch (Exception $e){
				exit('不支持gd');
			}
		}
		/**
		 * 设置源图像的地址并且返回图像信息并且  注意图片必须是本地的,
		 * 因为用这个函数获取远程的图片的效率是很低，因为这个函数要从网络上下载图片到本地，所以只适合在本地使用
		 * @param string $sourceImagesaddr
		 * @param bool	 $is_compressionImage  为true时进行图片压缩
		 */
		public function getsourceimagesize($sourceImagesaddr,$is_compressionImage=false){
			if (file_exists($sourceImagesaddr)) {
				$this->sourceImagesaddr=$sourceImagesaddr;
				$imagesize=getimagesize($this->sourceImagesaddr);
				$this->sourceImagesWidth=$imagesize[0];
				$this->sourceImagesHeight=$imagesize[1];
				$this->mime=$imagesize['mime'];
				$this->imagesExt='.'.str_replace('image/', '', $this->mime);
				if (!$is_compressionImage) {
					$this->imageTmp=imagecreatefromstring(file_get_contents($this->sourceImagesaddr));
					$this->compressionIamges();
				}
			}
		}
		/**
		 * 获取远程图片
		 * @param resouce $im
		 * @param bool	  $is_compressionImage  为true时进行图片压缩
		 */
		public function imageFormResouceGetSize($im,$curlinfo,$is_compressionImage=false){
			
				$imResouce=imagecreatefromstring($im);
				$this->sourceImagesHeight=imagesy($imResouce);
				$this->sourceImagesWidth=imagesx($imResouce);
				$this->mime=$curlinfo;
				$this->imagesExt='.'.str_replace('image/', '', $this->mime);
				if ($is_compressionImage) {
					$this->imageTmp=$imResouce;
					$this->compressionIamges();
	
			}
		}
		/**
		 * 进行压缩图片  
		 *
		 */
		public function compressionIamges(){
			$ratio_orig = $this->sourceImagesWidth/$this->sourceImagesHeight;
			 
			if ($this->compressionImagesWidth/$this->compressionImagesHieght > $ratio_orig) {
				$new_height = $this->compressionImagesWidth/$ratio_orig;
				$new_width = $this->compressionImagesWidth;
			} else {
				$new_width = $this->compressionImagesHieght*$ratio_orig;
				$new_height = $this->compressionImagesHieght;
			}
			 
			$x_mid = $new_width/2;  //horizontal middle
			$y_mid = $new_height/2; //vertical middle
			 
			$process = imagecreatetruecolor(round($new_width), round($new_height));
			
			imagecopyresampled($process, $this->imageTmp, 0, 0, 0, 0, $new_width, $new_height, $this->sourceImagesWidth, $this->sourceImagesHeight);
			$thumb = imagecreatetruecolor($this->compressionImagesWidth, $this->compressionImagesHieght);
			imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($this->compressionImagesWidth/2)), ($y_mid-( $this->compressionImagesHieght/2)), $this->compressionImagesWidth,  $this->compressionImagesHieght, $this->compressionImagesWidth,  $this->compressionImagesHieght);
			$this->imResouce=$thumb;
			imagedestroy($process);
			imagedestroy($this->imageTmp );
			
		}
		/**
		 * 将压缩后的图片输出的文件
		 * @param string $fileAddr
		 */
		public function outputFile($fileAddr){
			$this->outputfile=$fileAddr;
			switch ($this->mime) {
				case "image/gif":
					imagegif($this->imResouce,$fileAddr);
					break;
				case "image/jpeg":
					imagejpeg($this->imResouce,$fileAddr);
					break;
				case "image/png":
					imagepng($this->imResouce,$fileAddr);
					break;
				case "image/bmp":
					image2wbmp($this->imResouce,$fileAddr);
					break;
			}
		}
		/**
		 * 将压缩后的图片输出得到浏览器
		 */
		public function outputBrowser(){
			header('Content-type:'.$this->mime);
			imagegif($this->imResouce);
		}
		/**
		 * 销毁压缩后图片的资源 回收内存  
		 */
		public function destroyimage(){
			imagedestroy($this->imResouce);
		}
		/**
		 * 创建验证码
		 */
		public function cteateCaptcha(){
			(isset($_SESSION))?'':session_start();
			$captcha=null;
			//文件头...
			//创建真彩色白纸
			$im = @imagecreatetruecolor(100, 40) or die("建立图像失败");
			//获取背景颜色
			$background_color = imagecolorallocate($im, 250, 205, 205);
			//填充背景颜色(这个东西类似油桶)
			imagefill($im,0,0,$background_color);
			//获取边框颜色
			$border_color = imagecolorallocate($im,200,200,200);
			//画矩形，边框颜色200,200,200
			imagerectangle($im,0,0,100,40,$border_color);
			
			//逐行炫耀背景，全屏用1或0
			for($i=2;$i<40;$i++){
				//获取随机淡色
				$line_color = imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255));
				//画线
				imageline($im,0,$i,100,$i,$line_color);
			}
			
			//设置字体大小
			$font_size=28;
			
			//设置印上去的文字
			$Str[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			$Str[1] = "abcdefghijklmnopqrstuvwxyz";
			$Str[2] = "01234567891234567890123456";
			
			//获取第1个随机文字
			$imstr[0]["s"] = $Str[rand(0,2)][rand(0,25)];
			$imstr[0]["x"] = rand(2,5);
			$imstr[0]["y"] = rand(1,4);
			
			//获取第2个随机文字
			$imstr[1]["s"] = $Str[rand(0,2)][rand(0,25)];
			$imstr[1]["x"] = $imstr[0]["x"]+$font_size-1+rand(0,1);
			$imstr[1]["y"] = rand(1,3);
			
			//获取第3个随机文字
			$imstr[2]["s"] = $Str[rand(0,2)][rand(0,25)];
			$imstr[2]["x"] = $imstr[1]["x"]+$font_size-1+rand(0,1);
			$imstr[2]["y"] = rand(1,4);
			
			//获取第4个随机文字
			$imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)];
			$imstr[3]["x"] = $imstr[2]["x"]+$font_size-1+rand(0,1);
			$imstr[3]["y"] = rand(1,3);
			
			//写入随机字串
			for($i=0;$i<4;$i++){
				//获取随机较深颜色
				$text_color = imagecolorallocate($im,rand(50,180),rand(50,180),rand(50,180));
				//画文字
				imagestring($im,$font_size,$imstr[$i]["x"],rand(0, 20),$imstr[$i]["s"],$text_color);
				//imagettftext($im, $font_size, 0 , $imstr[$i]["x"], rand(0, 20), $text_color, font.ttf, $imstr[$i]["s"]);
				$captcha=$captcha.$imstr[$i]["s"];
			}
			$_SESSION['captcha']=$captcha;
			//显示图片
			header("Content-type: image/png");
			imagepng($im);
			//销毁图片
			imagedestroy($im);
		}
	}
?>